import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { FollowUpService } from './follow-up.service';

interface initInterface {
  type: string | number, // 1、图纸 2、充值
  businessNo: string // 关联的业务编码：订单号
}

@Component({
  selector: 'recharge-follow-up',
  templateUrl: './follow-up.component.html',
  styleUrls: ['./follow-up.component.scss']
})
export class RechargeFollowUpComponent implements OnInit {
  @Input() initParams!: initInterface; // {type: '',relationInformation: ''}
  @Output() deterMine = new EventEmitter(); // 确定提交

  // **************** Model *****************
  isModelVisible: boolean = false;
  uploadLoading: boolean = false;
  isConfirmLoading: boolean = false;
  content: string = ''; // 内容
  fileList: any = []; // 文件列表

  // table
  tableForms: any = {
    tableLoading: false,
    page: 1,
    pageSize: 10,
    total: 0
  };
  // 表格data
  modelTableData: any = [];

  constructor(
    private message: NzMessageService,
    private services: FollowUpService
  ) { }

  ngOnInit(): void {
  }

  async initData() {
    this.content = '';
    this.fileList = [];
    try {
      let params = {
        ...this.initParams
      }

      this.tableForms.tableLoading = true;
      const res: any = await this.services.queryFollowerListData(params);
      this.tableForms.tableLoading = false;
      if (res.code == 0) {
        const { records, total }: any = res.data;
        records.map((item: any) => {
          item.enclosure = (item.enclosure && item.enclosure.length) ? item.enclosure.split(',') : [];
        })
        this.modelTableData = records || [];
        this.tableForms.total = total || 0;
      } else {
        this.message.error(res.message || '数据获取失败');
      }
    } catch (error: any) {
      this.tableForms.tableLoading = false;
      this.message.error(error);
      this.modelTableData = [];
      this.tableForms.total = 0;
    }
  }

  showModal(): void {
    this.isModelVisible = true;
  }

  async handleOk() {
    if (!this.content) {
      this.message.warning('请输入内容');
      return
    }
    
    let params: any = {
      ...this.initParams,
      content: this.content,
      url: this.fileList.map((item: any) => item.url).join(',')
    }
    this.isConfirmLoading = true;
    const res: any = await this.services.saveFollowerItem(params);
    this.isConfirmLoading = false;
    if (res.code == 0) {
      this.message.success('操作成功');
      this.isModelVisible = false;
      // this.deterMine.emit('success');
      // 自定义提交操作
      this.deterMine.emit({ content: this.content, fileList: this.fileList });
    } else {
      this.message.error(res.message);
    }
  }

  handleCancel(): void {
    this.isModelVisible = false;
  }

  // 图片上传
  handleChange(ev: any) {
    const { originFileObj }: any = ev?.file;
    this.uploadInterface(originFileObj);
  }
  async uploadInterface(file: any) {
    if (!file || this.uploadLoading) {
      return
    }
    try {
      this.uploadLoading = true;
      const res: any = await this.services.uploadFileData(file, file.name)
      this.uploadLoading = false;

      if (res.code == 0) {
        this.message.success('上传成功');
        this.fileList.push({
          name: file.name,
          url: res.data
        });
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.uploadLoading = false;
      this.message.error(error);
    }
  }

  // 删除file
  deletFile(index: number) {
    this.fileList.splice(index, 1);
  }

  // 截取url
  interceptStr(str: string) {
    if (str) {
      str = str.substr(str.lastIndexOf('/') + 1)
    }
    return str || '-';
  }

  // 下载
  downFile(url: string) {
    window.open(url, '_blank');
  }

  // 上传文件夹字符串截取
  splitFileName(text: string) {
    var pattern: any = /\.{1}[a-z]{1,}$/;
    if (pattern.exec(text) !== null) {
      return (text.slice(0, pattern.exec(text).index));
    } else {
      return text;
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableForms.page = index;
    this.initData();
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableForms.page = 1;
    this.tableForms.pageSize = sizeNum;
    this.initData();
  }
}
